/*
 * Copyright 2007 Stefan Arentz <http://stefan.arentz.nl>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sateh.spaces.simple.matcher;

import com.sateh.spaces.simple.SimpleSpaceObject;
import com.sateh.spaces.simple.SimpleSpaceTemplate;
import com.sateh.spaces.simple.SimpleSpaceTransactionId;

public class SimpleSpaceObjectTemplateMatcher extends SimpleSpaceObjectClassMatcher
{
    private final SimpleSpaceTemplate template;

    public SimpleSpaceObjectTemplateMatcher(SimpleSpaceTemplate template, SimpleSpaceTransactionId transactionId)
    {
        super(template.getClassName(), transactionId);
        this.template = template;
    }

    public SimpleSpaceObjectTemplateMatcher(SimpleSpaceTemplate template)
    {
        super(template.getClassName(), null);
        this.template = template;
    }

    public boolean willMatch(SimpleSpaceObject simpleSpaceObject)
    {
        if (!super.willMatch(simpleSpaceObject)) {
            return false;
        }

        for (String propertyName : template.keys()) {
            if (!simpleSpaceObject.contains(propertyName) || !simpleSpaceObject.get(propertyName).equals(template.get(propertyName))) {
                return false;
            }
        }

        return true;
    }
}
